* Put database together by combining WIOD, OECD, JN, IFS, PWT, LV and WDI.
	* If WIOD and OECD are both available, give priority to WIOD
	* If WIOD/OECD and JN are avaiable, then:
		*- importshare2 gives priority to WIOD/OECD
		*- importshare  gives priority to JN
	
use "WIOD/output/WIOD_prep.dta", clear

* merge WIOD with OECD data, giving priority to WIOD 
merge 1:1 ccode year using  "OECD/output/OECD_prep.dta"
drop _merge
save "temp/WIODandOECDIntermed.dta", replace // this is WIOD/OECD

* merge WIOD/OECD with JN, giving priority to JN
	* remove the observations in WIOD/OECD for which there is a counterpart in JN.
	* necessary because WIOD has more years than JN; without this step, the data for 2010/11 would survive the merge
use "JN/output/JN.dta"
levelsof ccode, local(JNCountries)
use "temp/WIODandOECDIntermed.dta"
replace ccode="ROW" if ccode=="RoW"
replace ccode="ROM" if ccode=="ROU"
gen dropObs = 0
foreach n of local JNCountries {
	replace dropObs = 1 if (ccode=="`n'") & (year>=1995)
}
drop if dropObs==1
save "temp/WIODandOECD.dta", replace
* merge, giving JN priority
use "JN/output/JN.dta"
ren iexpshare importshare // homogenize names of import shares
ren fexpshare importshareC
save "temp/JN_prep.dta", replace
merge 1:1 ccode year using "temp/WIODandOECD.dta"
drop _merge
drop dropObs
save "temp/mergeJNpriority.dta", replace // database where JN has priority

*. merge WIOD/OECD with JN, giving priority to WIOD/OECD
use "temp/WIODandOECDIntermed.dta", clear
replace ccode="ROW" if ccode=="RoW"
replace ccode="ROM" if ccode=="ROU"
merge 1:1 ccode year using "temp/JN_prep.dta"
drop _merge
ren importshare importshare2 // name of vars with WIOD/OECD priority
ren importshareC importshareC2
sort ccode year
keep ccode year importshare2 importshareC2
merge 1:1 ccode year using "temp/mergeJNpriority.dta" // incorporate "importshare" (import share where JN has priority)
drop _merge
* Fix missing names in 2010/11
merge m:1 ccode using "LV/names.dta", update
drop _merge

// Fix some names
replace country="Bulgaria" if ccode=="BGR"
replace country="Bahrain" if ccode=="BRN"
replace country="Colombia" if ccode=="COL"
replace country="Costa Rica" if ccode=="CRI"
replace country="Cyprus" if ccode=="CYP"
replace country="Hong Kong" if ccode=="HKG"
replace country="Croatia" if ccode=="HRV"
replace country="Iceland" if ccode=="ISL"
replace country="Bahrain" if ccode=="BRN"
replace country="Cambodia" if ccode=="KHM"
replace country="Lithuania" if ccode=="LTU"
replace country="Luxembourg" if ccode=="LUX"
replace country="Latvia" if ccode=="LVA"
replace country="Malta" if ccode=="MLT"
replace country="Malaysia" if ccode=="MYS"
replace country="Philippines" if ccode=="PHL"
replace country="Saudi Arabia" if ccode=="SAU"
replace country="Singapore" if ccode=="SGP"
replace country="Tunisia" if ccode=="TUN"
replace country="Taiwan" if ccode=="TWN"

save "temp/WIOD_OECD_JN_temp.dta", replace

* Add IFS data
merge 1:1 ccode year using "IFS/output/IFS.dta"
keep if _merge==3 
drop _merge

* Add PWT data
merge 1:1 ccode year using "PWT/output/PWT_prep.dta"
keep if _merge==3
drop _merge

* Add LV data
merge 1:1 country year using  "LV/output/LV.dta"
drop if _merge==2
replace restructuring=0 if _merge==1
replace sovereign=0 if _merge==1
replace currency=0 if _merge==1
replace banking=0 if _merge==1
drop countryid _merge

* Add tariffs from WDI
replace ccode="ROU" if ccode=="ROM"
merge 1:1 ccode year using "WDITariffs/output/tariffs.dta"
drop if _merge==2
drop _merge
replace ccode="ROM" if ccode=="ROU"

save "output/Database.dta", replace
